home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / METAKIT.ZIP / EXAMPLES / MYIO / MYIO.CPP next >
Encoding:
C/C++ Source or Header  |  1996-12-09  |  4.8 KB  |  175 lines

  1. //  myio.cpp  -  Tricks to change the file I/O used within MetaKit
  2. //
  3. //  This is a part of the MetaKit library.
  4. //  Copyright (c) 1996 Meta Four Software.
  5. //  All rights reserved.
  6. /////////////////////////////////////////////////////////////////////////////
  7. //
  8. //  This code demonstrates:
  9. //
  10. //      - A class derived from c4_Strategy to implement encrypted storage.
  11. //      - Disabling the Flush calls issued during Commit() for speed.
  12. //      - Using c4_Strategy objects as the basis of all file I/O in MetaKit.
  13. //
  14. /////////////////////////////////////////////////////////////////////////////
  15.  
  16. #include "m4kit.h"
  17. #include "k4strat.h"
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #ifdef macintosh
  23.     #include <assert.h>
  24.     #define ASSERT assert
  25. #endif  
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // A helper class which owns a temporary buffer
  29.  
  30. class CTempBuffer
  31. {
  32. public:
  33.     CTempBuffer (int len_) : _buf (new char [len_]) { }
  34.     ~CTempBuffer () { delete [] _buf; }
  35.  
  36.     operator void* () const { return _buf; }
  37.     char& operator[] (int i) { return _buf[i]; }
  38.  
  39. private:
  40.     char* _buf;
  41. };
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // This derived strategy encrypts its data on disk and omits flushes
  45.  
  46. class CEncryptStrategy : public c4_Strategy
  47. {
  48. public:
  49.     CEncryptStrategy ()
  50.         : _pos (0) { }
  51.     virtual ~CEncryptStrategy ()
  52.         { /* nothing */ }
  53.  
  54.         // Need to track the file position to implement better encryption
  55.     virtual void DataSeek(long lOff)
  56.         { _pos = lOff; c4_Strategy::DataSeek(lOff); }
  57.  
  58.         // Reading and writing of course messes around with all the data
  59.     virtual int  DataRead(void*, int);
  60.     virtual void DataWrite(const void*, int);
  61.  
  62.         // For this example, we also disable all explicit file flushes
  63.     virtual void DataCommit(long)
  64.         { }
  65.  
  66. private:
  67.         // This example uses a trivial encoding.  The important aspect to
  68.         // consider is to make sure that seeks do not mess up the encoding.
  69.         // But as shown below, the offset of the data CAN be incorporated.
  70.     inline char Encode(char c_) const
  71.         { return (char) (c_ ^ _pos ^ 211); }
  72.     inline char Decode(char c_) const
  73.         { return (char) (c_ ^ _pos ^ 211); }
  74.  
  75.     long _pos;
  76. };
  77.  
  78. int CEncryptStrategy::DataRead(void* lpBuf, int nCount)
  79. {
  80.     int result = 0;
  81.  
  82.     if (nCount > 0)
  83.     {
  84.         CTempBuffer buf (nCount);
  85.         char* dest = (char*) lpBuf;
  86.  
  87.         result = c4_Strategy::DataRead(buf, nCount);
  88.  
  89.         for (int i = 0; i < result; ++i)
  90.         {
  91.             dest[i] = Decode(buf[i]);
  92.             ++_pos;
  93.         }
  94.     }
  95.  
  96.     return result;
  97. }
  98.  
  99. void CEncryptStrategy::DataWrite(const void* lpBuf, int nCount)
  100. {
  101.     if (nCount > 0)
  102.     {
  103.         CTempBuffer buf (nCount);
  104.         const char* src = (const char*) lpBuf;
  105.  
  106.         memcpy(buf, lpBuf, nCount);
  107.  
  108.         for (int i = 0; i < nCount; ++i)
  109.         {
  110.             buf[i] = Encode(src[i]);
  111.             ++_pos;
  112.         }
  113.  
  114.         c4_Strategy::DataWrite(buf, nCount);
  115.     }
  116. }
  117.  
  118. /////////////////////////////////////////////////////////////////////////////
  119.  
  120. int main()
  121. {
  122.         // This property could just as well have been declared globally.
  123.     c4_StringProp pLine ("line");
  124.  
  125.     {
  126.             // This is where the magic takes place.
  127.         CEncryptStrategy efile;
  128.         efile.DataOpen("secret.dat", true);
  129.         
  130.         c4_Storage storage (efile);
  131.  
  132.         static const char* message[] = {
  133.             "This is a small message which will be encrypted on file.",
  134.             "As a result, none of the other MetaKit utilities can read it.",
  135.             "Furthermore, a hex dump of this file will produce gibberish.",
  136.             "The encryption used here is ridiculously simple, however.",
  137.             "Beware of naive encryption schemes, cracking them is a sport.",
  138.             0
  139.         };
  140.  
  141.             // Store the text lines as separate entries in the view.
  142.         c4_View vText;
  143.  
  144.         for (const char** p = message; *p; ++p)
  145.             vText.Add(pLine [*p]);
  146.  
  147.         storage.Store("text", vText);
  148.         storage.Commit();
  149.     }
  150.  
  151.         // The end of the preceding block will flush out all data to file.
  152.  
  153.     {
  154.             // Repeat the process when accessing the encrypted file again.
  155.         CEncryptStrategy efile;
  156.         efile.DataOpen("secret.dat", false);
  157.  
  158.         c4_Storage storage (efile);
  159.         c4_View vText = storage.View("text");
  160.  
  161.         for (int i = 0; i < vText.GetSize(); ++i)
  162.         {
  163.             c4_String s = pLine (vText[i]);
  164.             puts(s);
  165.         }
  166.     }
  167.  
  168.         // At this point, an encrypted data file is left behind on the disk.
  169.  
  170.     return 0;
  171. }
  172.  
  173. /////////////////////////////////////////////////////////////////////////////
  174. // $Id: myio.cpp,v 1.5 1996/12/06 22:33:12 jcw Exp $
  175.